package com.siruier.boss.ui.activity.opencard

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.kaiKa
import com.siruier.boss.bean.City
import com.siruier.boss.databinding.ActivityOpenCardCitySelectBinding
import com.siruier.boss.databinding.ItemOpenCardCityBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.startActivityExt
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper

@SuppressLint("NotifyDataSetChanged")
class OpenCardCitySelectActivity : BaseActivity<ActivityOpenCardCitySelectBinding>() {
    private val isJdCity: Boolean by InjectBundle()
    private lateinit var mLoadService: LoadService<*>
    private val mProvinceAdapter = OpenCardCityAdapter()
    private val mCityAdapter = OpenCardCityAdapter()
    private var citys: MutableList<City>? = null

    override fun initView() {
        mLoadService = LoadSirHelper.register(vb.loadview, listener = ::onReload)

        mProvinceAdapter.setOnClickAdapterListenter(::onClick)
        vb.rvProvince.adapter = mProvinceAdapter
        vb.rvProvince.layoutManager = LinearLayoutManager(this)
        vb.rvProvince.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        mCityAdapter.setOnClickAdapterListenter(::onClickCity)
        vb.rvCity.adapter = mCityAdapter
        vb.rvCity.layoutManager = LinearLayoutManager(this)
        vb.rvCity.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        onReload()
    }

    private fun onReload() {
        launchUI({
            citys = if (isJdCity) {
                kaiKa.jdCityList().toData()
            } else {
                kaiKa.cityList().toData()
            }
            if (!citys.isNullOrEmpty()) {
                mProvinceAdapter.datas = citys!!
                mProvinceAdapter.notifyDataSetChanged()
            }
        }, ApiObserver(loadService = mLoadService))
    }

    private fun onClick(view: View, position: Int) {
        val province = mProvinceAdapter.getItem(position)
        if (province.child != null) {
            mCityAdapter.datas = province.child
            mCityAdapter.notifyDataSetChanged()
        }
        mProvinceAdapter.datas.forEachIndexed { index, city ->
            if (index != position && city.check) {
                city.check = false
                mProvinceAdapter.notifyItemChanged(index)
                return@forEachIndexed
            }
        }
        if (!province.check) {
            province.check = true
            mProvinceAdapter.notifyItemChanged(position)
        }
    }

    private fun onClickCity(view: View, position: Int) {
        val city = mCityAdapter.getItem(position)
        if (isJdCity) {
            setResult(RESULT_OK, Intent().apply {
                putExtra("province", mProvinceAdapter.datas.find { it.check })
                putExtra("city", city)
            })
            finish()
        } else {
            startActivityExt<OpenCardNumberSelectActivity> {
                putExtra("city", city)
            }
        }
    }
}

class OpenCardCityAdapter : BaseRecyclerAdapter<ItemOpenCardCityBinding, City>() {
    override fun onBindViewHolder(vb: ItemOpenCardCityBinding, bean: City, position: Int) {
        vb.tvText.setTextColor(if (bean.check) R.color.main_color.toColor() else R.color.text_color.toColor())
        vb.tvText.text = bean.name
        setViewClick(position, vb.root)
    }
}